import store from '@/store/index'
import router from "@/router";

//测试用数据
// let routerList = [
//     {
//         id: 1,
//         url: '/customer',
//         name: '客户管理',
//         isOnly: 0,
//         parentId: 0,
//
//     },
//     {
//         id: 2,
//         url:'123',
//         curl: 'navigation39',//主路径
//         name: '字典管理',//名称
//         isOnly: 0,//是否为单页面路径
//         parentId: 1,//父级id
//     },
//
//     {
//         id: 3,
//         url:'123',
//         curl: 'navigation39',//主路径
//         name: '字典管理',//名称
//         isOnly:1,//是否为单页面路径
//         parentId: 2,//父级id
//     },
//     {
//         id: 99,
//         url: '/router',
//         curl: 'navigation99',
//         name: '资源列表',
//         isOnly: 1,
//         parentId: 0,
//
//     }
// ]


// 用于处理动态菜单数据，将其转为 route 形式
import Layout from '@/layout'


function buildChildRoute(child) {
    return {
        path: child.curl,
        name: child.name,
        meta: {title: child.curl, isOnly: child.isOnly, id: child.id},
        // redirect: child.curl,
        // 添加try-catch处理动态import可能的异常
        component: () => {
            try {
                return import(/* webpackChunkName: "dashboard" */ `@/views/${child.curl}`);
            } catch (error) {
                console.error(`Failed to load component for ${child.curl}`, error);
                return Promise.reject(error);
            }
        },
        children: []
    };
}

function buildRouterTree(parentId, routerList) {
    const children = routerList.filter(child => child.parentId === parentId)
        .map(child => buildChildRoute(child));


    // 递归处理子路由
    children.forEach(child => {
        if (!child.meta.isOnly) {
            child.children = buildRouterTree(child.meta.id, routerList);
            if (child.children.length) {//三级路由重定向
                child.component = {
                    render: (c) => {
                        return c('router-view')
                    }
                }
            }
        }
    });

    return children;
}

export function setRouterFromNet(routerList) {
    routerList.forEach(item => {
        item.isOnly = parseInt(item.isOnly)
    })
    // let a = buildTree(routerList,0)
    //  console.log('this is router tree',a)
    const totalRouter = []
    routerList.forEach(item => {
        //找出父级
        if (!item.isOnly && item.parentId === 0) {
            let obj = {
                path: item.url,
                name: item.name,
                component: Layout,
                meta: {isOnly: item.isOnly},
                children: []
            }

            //过滤子旅游
            obj.children = buildRouterTree(item.id, routerList)

            totalRouter.push(obj)
        } else if (item.isOnly && item.parentId === 0) {//独立路由
            let aloneObj = {
                path: item.url,
                name: item.name,
                component: Layout,
                meta: {isOnly: item.isOnly},
                children: [
                    {
                        path: item.curl,
                        name: item.name,
                        meta: {title: item.curl},
                        component: () => import(/* webpackChunkName: "dashboard" */`@/views/${item.curl}`),
                    }
                ]
            }
            totalRouter.push(aloneObj)
        }
    })
    router.addRoutes(totalRouter)//添加路由
    //添加vuex用于菜单
    console.log(totalRouter)

    store.dispatch('dynamicRoutes', totalRouter).then(() => {
    })


}
